<?php

namespace Admin\Controller;

class UserController extends AdminCoreController {
    
	public $Model = null;

    protected function _initialize(){
		
        parent::_initialize();
		
        $this->Model = D('User');
		
    }
	
	public function index(){
		
		if(IS_AJAX){
						
			$mark = I('get.mark');
			
			$this->assign('mark', $mark);
			
			$search = I('get.search');
			
			if (!empty($search)){
				
                $map[$mark] = array('like', "%$search%");
				
			}
			
			$page_size = 10;
			
			$count = $this->Model->where($map)->count();
			
			$Page = new \Think\Page($count, $page_size);
			
			$list = $this->Model
				->where($map)
				->order("id desc")
				->limit($Page->firstRow . ',' . $Page->listRows)
				->select();
				
			foreach($list as $key => $v){
				
				if($v['create_time'] == 0) {
					
                    $list[$key]['create_time'] = '';
					
                }else{
					
					$list[$key]['create_time'] = date('Y-m-d',$list[$key]['create_time']);
					
				}
				
				if($v['update_time'] == 0) {
					
                    $list[$key]['update_time'] = '';
					
                }else{
					
					$list[$key]['update_time'] = date('Y-m-d',$list[$key]['update_time']);
					
				}
				
                if($v['last_login_time'] == 0) {
					
                    $list[$key]['last_login_time'] = '';
					
                }else{
					
					$list[$key]['last_login_time'] = date('Y-m-d',$list[$key]['last_login_time']);
					
				}
				
				if($v['status'] == 0) {
					
                    $list[$key]['status'] = '<span class="button button-danger btn-audit" title="管理员状态">禁用</span>';
					
                }else{
					
					$list[$key]['status'] = '<span class="button button-success btn-audit" title="管理员状态">启用</span>';
					
				}
				
            }
			
			$str = $this->tojson($list, $count);
			
			echo $str;
			
			exit;
			
        }else{
			
            $this->display();
			
        }
    }
	
	public function checkname(){
		
		$id = I('get.id');
					
		$name = I('post.name');
			
		$username = I('post.param');
			
		if($id){
			
			$map['id'] = array('neq', $id);
		
			$map['username'] = array('eq', $username);
			
			$data = $this->Model->where($map)->select();
			
		}else{
			
			$data = $this->Model->where(array('username'=>$username))->find();
			
		}
			
		if(!$data){
				
			echo 'y';
			 
		}else {
				
			echo '此用户名称已被占用';
				
		}
		
	}
	
	public function add(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$data = $this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->update($data);
				
				if($result){
					
					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
					
				}else{
					
					$error = $this->Model->getError();
					
					echo json_encode(array('info'=>$error, 'status'=>'n'));
					
				}
				
			}else{
				
				$error = $this->Model->getError();
				
				echo json_encode(array('info'=>$error, 'status'=>'n'));
				
			}	
					
		}else{
			
			$this->display();
			
		}
		
	}
	
	public function edit(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$data=$this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->where(array('id'=>$post_data['id']))->update($data);
				
				if($result){
					
					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
					
				}else{
					
					$error = $this->Model->getError();
					
					echo json_encode(array('info'=>$error, 'status'=>'n'));
					
				}
				
			}else{
				
                $error = $this->Model->getError();
				
                echo json_encode(array('info'=>$error, 'status'=>'n'));
				
			}
			
		}else{
			
			$info=I('get.');
			
			$info = $this->Model->where(array('id'=>$info['id']))->find();
			
			$this->assign('info', $info);
			
        	$this->display();
			
		}
		
	}
	
	public function repwd(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$old = $this->Model->where(array('id'=>$post_data['id']))->find();
			
			$post_data['passwordold'] = md5($secure_code . md5($post_data['passwordold']));
			
			$post_data['password'] = md5($secure_code . md5($post_data['password']));
			
			$post_data['repassword'] = md5($secure_code . md5($post_data['repassword']));
			
			if($old['password'] != $post_data['passwordold']){
				
				echo json_encode(array('info'=>'旧密码输入不正确', 'status'=>'n'));
				
			}elseif($post_data['password'] != $post_data['repassword']){
				
				echo json_encode(array('info'=>'两次密码输入不一致', 'status'=>'n'));
				
			}else {
			
				$data=$this->Model->create($post_data);
				
				if($data){
					
					$result = $this->Model->where(array('id'=>$post_data['id']))->update($data);
					
					if($result){
						
						echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
						
					}else{
						
						$error = $this->Model->getError();
						
						echo json_encode(array('info'=>$error, 'status'=>'n'));
						
					}
					
				}else{
					
					$error = $this->Model->getError();
					
					echo json_encode(array('info'=>$error, 'status'=>'n'));
					
				}
			
			}
			
		}else{
			
			$info=I('get.');
			
			$info = $this->Model->where(array('id'=>$info['id']))->find();
			
			$this->assign('info', $info);
			
        	$this->display();
			
		}
		
	}
	
	public function audit(){
				
		$ids = I('get.ids');
		
		$map['id'] = array('in', $ids);
		
		$data = $this->Model->where($map)->find();
		
		if($data['status'] == 0){
			
			$post_data['status'] = 1;
			
		}
		
		if($data['status'] == 1){
			
			$post_data['status'] = 0;
			
		}
		
		$res = $this->Model->where($map)->save($post_data);
			
		if($res){
			
            echo json_encode(array('info'=>'开启状态成功', 'status'=>'y'));
			
        }else{
			
            $error = $this->Model->getError();
			
			echo json_encode(array('info'=>$error, 'status'=>'n'));
			
        }
		
	}
    
    public function del(){
		
		$ids = I('get.ids');
		
        $map['id'] = array('in', $ids);
		
        $res = $this->Model->where($map)->delete();
		
        if($res){
			
            echo json_encode(array('info'=>'删除成功', 'status'=>'y'));
			
        }else{
			
            $error = $this->Model->getError();
			
			echo json_encode(array('info'=>$error, 'status'=>'n'));
			
        }
		
	}

}